package com.controller;

import com.pojo.Account;
import com.pojo.RightClassOne;
import com.pojo.Role;
import com.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/addBusinessController")
public class AddBusinessRole {

    @Autowired
    private RoleService roleService;

    /**
     * toRoleAddBusiness
     * 修改商户页面，需要跳转控制器
     */
    @RequestMapping("/showSuperRoleList")
    public String showSuperRoleList(HttpSession session) {
        session.setAttribute("showSuperRoleList", roleService.superBusinessList());  //父级商户列表
        System.out.println("showSuperRoleList:" + roleService.superBusinessList());
        return "redirect:/AddRoleChilds.jsp";
    }


    /**
     * toRoleAddBusiness
     * 修改商户页面，需要跳转控制器
     */
    @RequestMapping("/findthisRole")
    public String findthisRole(Long roleId, HttpSession session) {
        session.setAttribute("showSuperRoleList", roleService.superBusinessList());  //父级商户列表
        System.out.println("showSuperRoleList:" + roleService.superBusinessList());
        session.setAttribute("thisRole", roleService.showRoleMsg(roleId));   //查询需要修改的商户信息
        System.out.println("addBusinessController中findthisRole获得的thisRole值：" + session.getAttribute("thisRole"));
        return "redirect:/UpdateRole.jsp";
    }

    /**
     * findAccountRights：查询所有菜单权限
     */
    @RequestMapping("/findAllRights")
    @ResponseBody
    public List<RightClassOne> findAllRights() {
        return roleService.findAllRights();
    }

    /**
     * findAccountRights：查询当前角色的所有菜单权限_批量选中
     */
    @RequestMapping("/findRoleRights")
    @ResponseBody
    public List<RightClassOne> findRoleRights(HttpSession session) {
        Role thisRole = (Role) session.getAttribute("thisRole");
        return roleService.findAccountRights(thisRole.getRoleId());
    }

    /**
     * findAccountRights：查询当前角色的所有操作权限_批量选中
     */
    @RequestMapping("/findRoleDoRights")
    @ResponseBody
    public List<RightClassOne> findRoleDoRights(HttpSession session) {
        Role thisRole = (Role) session.getAttribute("thisRole");
        return roleService.findAccountDoRights(thisRole.getRoleId());
    }

    /**
     * findAccountRights：查询所有操作权限
     */
    @RequestMapping("/findAllDoRights")
    @ResponseBody
    public List<RightClassOne> findAllDoRights() {
        return roleService.findAllDoRights();
    }

    /**
     * 修改角色信息
     *
     * @param role
     * @return
     */
    @RequestMapping("/updateBusiness")
    @ResponseBody
    public String updateBusiness(Role role, HttpSession session) {
        Role thisRole = (Role) session.getAttribute("thisRole");
        role.setRoleId(thisRole.getRoleId());
        if (roleService.updateBusiness(role)) {
            return "1";
        }
        return "0";
    }


    /**
     * 添加主商户基本信息
     *
     * @param role
     * @return
     */
    @RequestMapping("/addBusinessRoleMsg")
    @ResponseBody
    public String addBusiness(Role role, HttpSession session) {
        System.out.println("需要添加的role的值：" + role);
        if (roleService.addBusiness(role)) {
            role = roleService.findAddRole(role.getRoleName());  //查询添加的角色信息，获取roleid
            session.setAttribute("addRoleId", role.getRoleId());
            return "1";
        }
        return "0";
    }

    /**
     * 添加主商户权限
     *
     * @param rights
     * @return
     */
    @RequestMapping("/addBusinessRights")
    @ResponseBody
    public String addBusinessRights(String rights, HttpSession session) {
        Long addRoleId = (Long) session.getAttribute("addRoleId");   //增加的角色id
        System.out.println("需要新增权限的新商户id" + addRoleId);
        if (rights.length()>2) {
            rights = rights.substring(1, rights.length() - 1); //去掉数组的[]
            if (roleService.addRoleRights(rights, Long.valueOf(addRoleId))) {
                return "1";
            }
        }else{
            return "1";
        }
        return "0";
    }

    /**
     * findAccountRights：查询当前角色的所有菜单权限_批量选中
     */
    @RequestMapping("/findAccountRights")
    @ResponseBody
    public List<RightClassOne> findAccountRights(HttpSession session) {
        Account thisRole = (Account) session.getAttribute("thisAccount");
        return roleService.findAccountRights(thisRole.getRoleId());
    }

    /**
     * findAccountRights：查询当前角色的所有操作权限_批量选中
     */
    @RequestMapping("/findAccountDoRights")
    @ResponseBody
    public List<RightClassOne> findAccountDoRights(HttpSession session) {
        Account thisRole = (Account) session.getAttribute("thisAccount");
        return roleService.findAccountDoRights(thisRole.getRoleId());
    }
}
